home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-21 | 11.1 KB | 471 lines | [TEXT/MPS ] |
- /*
- File: TestTool.cp
-
- Contains: MPW Tool to test out Classes.
-
- Description:
-
- TestTool [-v] [-t] [-c nReps] [className]...
-
- -v turns verbose mode on (progress messages in MPW window), it is off by default
- -t turns tracing on (tracing to the Trace Monitor), it is off by default
- -c nReps nReps must be a positive integer for number of times through the test while loop
-
-
- This test program can be modified to test any tool you develop and do simple stress
- testing by changing what is inside the test while loop.
-
- Caveats:
-
- Command-period will leave some things dangling that will never be disposed.
-
-
- Copyright: © 1991-1993 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
- #ifndef __TESTTOOL__
- #include <TestTool.h>
- #endif
- #ifdef USEMPW
- #ifndef __MPWSHAREDLIBS__
- #include <MPWSharedLibs.h>
- #endif
- #endif
- #ifndef __LIBRARYMANAGERCLASSES__
- #include <LibraryManagerClasses.h>
- #endif
- #ifndef __LIBRARYMANAGERUTILITIES__
- #include <LibraryManagerUtilities.h>
- #endif
-
- #ifndef __QUICKDRAW__
- #include <QuickDraw.h>
- #endif
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
- #ifndef __CTYPE__
- #include <ctype.h>
- #endif
- #ifndef __IOSTREAM__
- #include <iostream.h>
- #endif
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
- #ifndef __CURSORCTL__
- #include <CursorCtl.h>
- #endif
-
- static int getIntArgument(char *arg);
- static void Idle(RgnHandle);
- #ifndef USEMPW
- static int myPrintFunc(const char*, char*);
- #endif
-
- const size_t kTestPoolSize = 10000;
-
- main(int argc, char *argv[])
- {
-
- int idx;
- int argIdx;
- int theCount = 1;
- int numClasses = 0;
- Boolean verbose = false;
- Boolean trace = false;
- Boolean debugging = false;
- Boolean nogrowpool = false;
- Boolean doAll = false;
- Boolean doLoad = true;
- TPoolNotifier* savedNotifier = NULL;
- RgnHandle myRgn;
- char* classes[50];
-
- InitGraf(&qd.thePort); // initialize quickdraw so we can use regions
- myRgn = NewRgn(); // a region to use for WaitNextEvent
-
- InitCursorCtl(NULL);
-
- #ifdef USEMPW
- InitLibraryManager(kTestPoolSize, kApplicZone);
- TLibraryManager* testMgr = GetLocalLibraryManager();
-
- if (testMgr == NULL)
- {
- cout << "### ERROR: Could not allocate a TLibraryManager" << endl;
- return 1;
- }
- //
- // %%% Initialize MPW Library Callbacks
- //
- if (InitStdIOCallbacks() != 0)
- {
- cout << "### ERROR: Could not initialize StdIO callbacks" << endl;
- return 1;
- }
- #endif
-
- /* -----------------------------------------------------------------
- Parse the arguments
- ----------------------------------------------------------------- */
-
- if (argc < 2)
- {
- fprintf(stderr, "# Usage - %s [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]\n", argv[0]);
- fprintf(stderr, " -v Verbose\n");
- fprintf(stderr, " -x Debugging ON (default is OFF)\n");
- fprintf(stderr, " -p Don't grow pool\n");
- fprintf(stderr, " -t Trace facility ON (default is OFF\n");
- fprintf(stderr, " -s Shutdown the ASLM\n");
- fprintf(stderr, " -nl Don't do a Load on the classes\n");
- fprintf(stderr, " -l Load the ASLM\n");
- fprintf(stderr, " -a Do all tests\n");
- fprintf(stderr, " -n <Count> Iteration count\n");
- fprintf(stderr, " -c <YourTestTool> the name of a TTestTool subclass to instantiate\n");
- fprintf(stderr, " -o … Remaining arguments are passed to <YourTestTool>::InitTest\n");
- return 1;
- }
-
- for (idx = 1; idx < argc && argv[idx][0] == '-' && argv[idx][1] != 'o'; idx++)
- {
- if (argv[idx][0] == '-')
- {
- switch (tolower(argv[idx][1]))
- {
- case 'a':
- if (numClasses != 0)
- {
- fprintf(stderr, "### -a and -c are mutually exlusive switches!\n");
- return 1;
- }
- doAll = true;
- break;
-
- case 'n':
- if (tolower(argv[idx][2]) == 'l')
- {
- doLoad = false;
- }
- else
- if (++idx < argc)
- {
- theCount = getIntArgument(argv[idx]);
- if (theCount < 0)
- {
- fprintf(stderr, "### option -n value is not a positive integer: %s\n",
- argv[idx]);
- return 1;
- }
- }
- else
- {
- fprintf(stderr, "### Not enough arguments\n");
- return 1;
- }
- break;
-
- case 'l':
- if (verbose)
- cout << "Loading the ASLM" << endl;
- if (!LoadLibraryManager())
- {
- cout << "### ERROR: the ASLM could not be loaded!\n";
- return 10;
- }
- return 0;
-
- case 'v':
- verbose = true;
- break;
-
- case 't':
- trace = true;
- break;
-
- case 's':
- if (verbose)
- cout << "Unloading the ASLM" << endl;
- UnloadLibraryManager();
- if (idx+1 < argc && tolower(*(argv[idx+1]+1)) == 'l')
- if (!LoadLibraryManager())
- {
- cout << "### ERROR: the ASLM could not be loaded!\n";
- return 10;
- }
- return 0;
-
- case 'p':
- nogrowpool = true;
- break;
-
- case 'x':
- debugging = true;
- break;
-
- case 'c':
- if (doAll)
- {
- fprintf(stderr, "### -a and -c are mutually exlusive switches!\n");
- return 1;
- }
- idx++;
- classes[numClasses] = new char[strlen(kTestToolPrefix) +
- strlen(argv[idx]) + 1];
- strcpy(classes[numClasses], kTestToolPrefix);
- strcat(classes[numClasses++], argv[idx]);
- break;
-
- default:
- fprintf(stderr, "# Usage - %s [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]\n", argv[0]);
- fprintf(stderr, " -v Verbose\n");
- fprintf(stderr, " -x Debugging ON (default is OFF)\n");
- fprintf(stderr, " -p Don't grow pool\n");
- fprintf(stderr, " -t Trace facility ON (default is OFF\n");
- fprintf(stderr, " -s Shutdown the ASLM\n");
- fprintf(stderr, " -l Load the ASLM\n");
- fprintf(stderr, " -a Do all tests\n");
- fprintf(stderr, " -n <Count> Iteration count\n");
- fprintf(stderr, " -c <YourTestTool> the name of a TTestTool subclass to instantiate\n");
- fprintf(stderr, " -o … Remaining arguments are passed to <YourTestTool>::InitTest\n");
- return 1;
-
- }
- }
- }
-
- if (argv[idx][0] == '-' && argv[idx][1] == 'o')
- idx++;
-
- argc -= idx;
-
- argIdx = idx;
-
- #ifndef USEMPW
- if (debugging)
- DebugStr("\pAbout to call InitLibraryManager");
-
- InitLibraryManager(kTestPoolSize, kApplicZone);
- TLibraryManager* testMgr = GetLocalLibraryManager();
-
- if (testMgr == NULL)
- {
- cout << "### ERROR: Could not allocate a TLibraryManager" << endl;
- return 1;
- }
- #endif
-
- if (doAll)
- {
- OSErr err;
-
- numClasses = 0;
-
- TClassInfo* info = testMgr->GetClassInfo(ClassID(kTTestToolID), &err);
- if (info == NULL)
- {
- cout << "### ERROR: Could not get a TClassInfo (err = )" << err << endl;
- CleanupLibraryManager();
- return 1;
- }
- char* str;
- while (str = (char*)info->Next())
- {
- classes[numClasses] = new char[strlen(str) + 1];
- strcpy(classes[numClasses++], str);
- }
- delete info;
- }
-
-
- TStandardPool* thePool = GetLocalPool();
-
- savedNotifier = thePool->GetNotifier();
- if (nogrowpool)
- thePool->SetNotifier(NULL);
-
- PoolInfo info;
- thePool->GetPoolInfo(info);
- cout << "INFO: Pool size = " << info.fFreeBytes << endl;
- cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
-
- // turn trace on or off according to -t option
- if (!trace)
- testMgr->TraceLogOff();
- else
- testMgr->TraceLogOn();
-
- // dump out TLibraryManager info
- testMgr->Dump();
-
- for (idx = 0; idx < numClasses; ++idx)
- {
- TTestTool* tool = NULL;
-
- Idle(myRgn);
-
- if (verbose)
- cout << "INFO: Creating Tool " << classes[idx] << endl;
-
-
- OSErr theErr;
-
- if (doLoad)
- {
- if (debugging)
- DebugStr("\pAbout to load class");
- theErr = testMgr->LoadClass(ClassID(classes[idx]), true);
- if (theErr != kNoError)
- {
- fprintf(stderr, "###LoadClass failed, error = %d\n", theErr);
- continue;
- }
- }
- else
- if (debugging)
- DebugStr("\pAbout to new tool");
- // LoadClass succeeded
- tool = (TTestTool*)testMgr->NewObject(ClassID(classes[idx]), ClassID(kTTestToolID), &theErr);
- if (theErr != kNoError)
- fprintf(stderr, "###NewObject Error %d\n", theErr);
-
- if (tool == NULL)
- cout << "### ERROR: Could not create Tool " << classes[idx] << endl;
- else
- {
- tool->SetPool(thePool);
- cout.flush();
- #ifndef USEMPW
- tool->SetPrintf(myPrintFunc);
- #endif
-
- int count = theCount;
-
- if (verbose)
- {
- PoolInfo info;
- thePool->GetPoolInfo(info);
- cout << "INFO: Pool size = " << info.fFreeBytes << endl;
- cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
- }
-
- tool->InitTest(verbose, debugging, argc, argv+argIdx);
- Idle(myRgn);
- while (count--)
- {
- tool->RunTestIteration(verbose, debugging);
- Idle(myRgn);
- }
- tool->EndTest(verbose, debugging);
- fflush(stdout);
- Idle(myRgn);
-
- if (verbose)
- cout << "INFO: Destroying Tool " << classes[idx] << endl;
-
- if (debugging)
- DebugStr("\pAbout to destroy tool");
-
- delete tool;
- if (verbose)
- {
- PoolInfo info;
- thePool->GetPoolInfo(info);
- cout << "INFO: Pool size = " << info.fFreeBytes << endl;
- cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
- if (nogrowpool)
- {
- void* test = thePool->Allocate(kTestPoolSize*2);
- if (test != NULL)
- {
- cout << "ERROR: Allocated block larger than pool size!" << endl;
- thePool->GetPoolInfo(info);
- cout << "ERROR: Pool size = " << info.fFreeBytes << endl;
- cout << "ERROR: Largest Block = " << info.fLargestBlock << endl;
- thePool->Free(test);
- thePool->GetPoolInfo(info);
- cout << "ERROR: Pool size = " << info.fFreeBytes << endl;
- cout << "ERROR: Largest Block = " << info.fLargestBlock << endl;
- }
- else
- {
- thePool->GetPoolInfo(info);
- cout << "INFO: After freeing chunks -->" << endl;
- cout << "INFO: Pool size = " << info.fFreeBytes << endl;
- cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
- }
- }
- }
- Idle(myRgn);
- }
-
- if (doLoad)
- testMgr->UnloadClass(ClassID(classes[idx]));
- }
-
- if (verbose && numClasses > 1)
- {
- PoolInfo info;
- thePool->GetPoolInfo(info);
- cout << "INFO: Pool size = " << info.fFreeBytes << endl;
- cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
- }
-
- if (nogrowpool)
- thePool->SetNotifier(savedNotifier);
-
- if (verbose)
- cout << "INFO: Disposing testMgr" << endl;
- CleanupLibraryManager(); // delete the TLibraryManager and its pool
-
- DisposeRgn(myRgn);
-
- return 0;
- };
-
- /**********************************************************************
- ** STATIC Functions
- ***********************************************************************/
-
- #ifndef USEMPW
- static int myPrintFunc(const char* format, char* args)
- {
- int ret;
- ret = vprintf(format, args);
- fflush(stdout);
- return ret;
- }
- #endif
-
- static int getIntArgument(char *arg)
- {
- int theCount = 0;
- Boolean sign = false;
-
- while (*arg == ' ')
- ++arg;
- if (*arg == '-')
- {
- sign = true;
- ++arg;
- }
- else
- if (*arg == '+')
- ++arg;
-
- while (*arg >= '0' && *arg <= '9')
- theCount = theCount*10 + (*arg++) - '0';
- if (sign)
- theCount = -theCount;
- return theCount;
- }
-
- static void Idle(RgnHandle myRgn)
- {
- EventRecord myEvent;
- WaitNextEvent(nullEvent, &myEvent, 0, myRgn);
- }